package com.hxy.quartz.job;

import com.hxy.order.service.IAdoptOrderService;
import com.hxy.order.service.IProductOrderService;
import com.hxy.quartz.constant.JobConstants;
import com.hxy.quartz.dto.QuartzJobInfo;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

public class MainJob implements Job {
    @Autowired
    private IAdoptOrderService adoptOrderService;
    @Autowired
    private IProductOrderService productOrderService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //与工具类中的add方法中的params一致
        QuartzJobInfo info = (QuartzJobInfo)jobExecutionContext.getJobDetail().getJobDataMap().get("params");
        String type = info.getType();
        //根据不同的类型 type 区分不同的业务
        switch (type){
            //超时未支付 = 取消订单
            case JobConstants.OVER_TIME_NO_PAY_ADOPT:{
                Map<String, Object> params = info.getParams();
                Object orderId = params.get("orderId");
                if(orderId!=null){
                    long oid = Long.parseLong(orderId.toString());
                    //根据old取消订单 = 设置state = -1
                    adoptOrderService.cancelOrder(oid);
//                    productOrderService
                }
                break;
            }
            //超时未确认 = 自动确认
            case JobConstants.OVER_TIME_NO_CONFIRM_ADOPT:{
                break;
            }
            case JobConstants.OVER_TIME_NO_PAY_PRODUCT:{
                Map<String, Object> params = info.getParams();
                Object orderId = params.get("orderId");
                if (orderId != null){
                    long oid = Long.parseLong(orderId.toString());
                    productOrderService.cancelOrder(oid);
                }
                break;
            }
        }
    }
}
